//
//  IntelligentTriageController.m
//  HarmonizePub
//
//  Created by Haven on 16/10/12.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "IntelligentTriageController.h"
#import "Config.h"
#import "IntelligentRotateButton.h"
#import "UIView+extension.h"
#import "IntelligentBody.h"
#import "MJExtension.h"
#import "GuideSymptomViewController.h"
#import "GuideDiagnoseViewController.h"

// 图像正面反面
typedef NS_ENUM(NSUInteger, TurnButtonType) {
    
    TurnButtonTypeOpposite     = 0, // 反面
    TurnButtonTypeFace     = 1, // 正面

};
// 按钮类型
typedef NS_ENUM(NSUInteger, ButtonType) {
    ButtonTypeMan     = 0, // 男
    ButtonTypeWoman   = 1, // 女
    ButtonTypeChild   = 2, // 儿童
};

#define ScreenW [UIScreen mainScreen].applicationFrame.size.width
#define ScreenH [UIScreen mainScreen].applicationFrame.size.height

@interface IntelligentTriageController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segMentControl;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) UIImageView *imageView; // 第一页ImageView
@property (nonatomic, strong) UIButton *turnBtn; // 转身按钮
@property (nonatomic, strong) UIView *firstView; // 第一页UIView
@property (nonatomic, strong) UITableView *tableView; // 部位列表
@property (nonatomic, assign) TurnButtonType turnBtnType; // 图片正面/反面
@property (nonatomic, assign) ButtonType buttonType; // 按钮类型
@property (nonatomic, strong) NSArray *bodyLists;
@property (nonatomic, strong) UIButton *childBtn;

@end

@implementation IntelligentTriageController
- (NSArray *)bodyLists
{
    if (_bodyLists == nil) {
        _bodyLists = [[NSArray alloc] init];
    }
    return _bodyLists;
}


- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"智能分诊";
    self.segMentControl.selectedSegmentIndex=0;
    self.turnBtnType = TurnButtonTypeFace;
    self.buttonType = ButtonTypeMan;

    // 创建控件
    [self createUICotrol];
    
    // 加载部位列表
    [self loadBodyList];
}

// 创建控件
- (void)createUICotrol
{
    // 设置scrollView属性
    [self.scrollView setContentSize:CGSizeMake(ScreenW*2, 0)];
    [self.scrollView setFrame: CGRectMake(0, 75, ScreenW, ScreenH - 70 - 64)];
    
    // 创建第一页View
    self.firstView=[[UIView alloc] initWithFrame: CGRectMake(0, 0, ScreenW, self.scrollView.bounds.size.height + 20)];
    self.firstView.userInteractionEnabled = YES;
    self.firstView.backgroundColor = [UIColor whiteColor];
    
    [self.scrollView addSubview:self.firstView];
    
    // 创建第二页
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(ScreenW + 10, 0, ScreenW - 20, self.scrollView.bounds.size.height + 20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.scrollView addSubview:self.tableView];
    
    // 创建图像imageView
    self.imageView=[[UIImageView alloc] initWithFrame:CGRectMake(60, 0, self.firstView.bounds.size.width - 120, self.firstView.bounds.size.height)];
    self.imageView.image = [UIImage imageNamed:@"男正面"];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.userInteractionEnabled = YES;
    [self.firstView addSubview:self.imageView];
    
    // imageView上添加点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTouch:)];
    [self.imageView addGestureRecognizer:tap];
    
    // 创建旋转按钮及文字
    self.turnBtn = [IntelligentRotateButton IntelligentRotateButton];
    self.turnBtn.height = 60;
    CGRect btnFrame = self.turnBtn.frame;
    btnFrame.origin = CGPointMake(CGRectGetMaxX(self.imageView.frame) + 5, self.firstView.bounds.size.height - self.turnBtn.height - 5);
    self.turnBtn.frame = btnFrame;
    [self.turnBtn addTarget:self action:@selector(turnBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.firstView addSubview:self.turnBtn];
    
    // 创建男, 女, 儿童按钮图片切换按钮,  暂不实现儿童功能
    CGFloat btnW = 36;
    CGFloat btnMaginLeft = 10;
    CGFloat manBtnY = 40;
    CGFloat btnMagin = 50;
    for (int i = 0; i < 3; i ++) {
        UIButton *Btn = [[UIButton alloc] initWithFrame:CGRectMake(btnMaginLeft, btnMagin*i + manBtnY, btnW, btnW)];
        Btn.tag = i;
        [Btn addTarget:self action:@selector(clickBtnBtn:) forControlEvents:UIControlEventTouchDown];
        [Btn setBackgroundColor: [UIColor whiteColor]];
        Btn.titleLabel.font = [UIFont systemFontOfSize:14];
        if (i == 0) {
            [Btn setImage:[UIImage imageNamed:@"男"] forState:UIControlStateNormal];
        }else if ( i == 1){
            [Btn setImage: [UIImage imageNamed:@"女"] forState:UIControlStateNormal];
        }else if (i == 2){
            Btn.hidden = YES;
            [Btn setTitle:@"儿童" forState:UIControlStateNormal];
        }
        [self.firstView addSubview:Btn];
    }
}

// 切换图片
- (void)clickBtnBtn: (UIButton *)button
{
    if (button.tag == 0) {
        self.imageView.image=[UIImage imageNamed:@"男正面.png"];
        self.turnBtnType = TurnButtonTypeFace;
        self.buttonType = ButtonTypeMan;
    }else if (button.tag == 1){
        self.imageView.image=[UIImage imageNamed:@"女正面.png"];
        self.turnBtnType = TurnButtonTypeFace;
        self.buttonType = ButtonTypeWoman;
    }else if (button.tag == 2){
        self.imageView.image=[UIImage imageNamed:@"儿童正面.png"];
        self.turnBtnType = TurnButtonTypeFace;
        self.buttonType = ButtonTypeChild;
    }
}

// 根据图片位置判断分诊的内容
- (void)imageViewTouch: (UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint touchPoint = [gestureRecognizer locationInView:self.imageView];
    CGFloat x=touchPoint.x;
    CGFloat y=touchPoint.y;
//    XHLog(@"x=%f----y=%f", x, y);
    CGFloat mainHeight = [UIScreen mainScreen].bounds.size.height;
    if (mainHeight > 568) { // 6以上手机
        if (self.turnBtnType == TurnButtonTypeFace) {
            if ((x >= 110 && x <= 175) && (y >= 40 && y <= 80)) {
                [self loadDataBodyList: @"头部"];
            }else if ((x >= 110 && x <= 175) && (y >= 81 && y <= 120)){
                [self loadDataBodyList: @"耳眼口鼻"];
            }else if ((x >= 125 && x <= 167) && (y >= 122 && y <= 142)){
                [self loadDataBodyList: @"颈部"];
            }else if ((x >= 100 && x <= 200) && (y >= 160 && y <= 205)){
                [self loadDataBodyList: @"胸部"];
            }else if ((x >= 110 && x <= 170) && (y >= 215 && y <= 270)){
                [self loadDataBodyList: @"腹部"];
            }else if ((x >= 117 && x <= 170) && (y >= 280 && y <= 328)){
                [self loadDataBodyList: @"生殖器"];
            }else if (((x >= 35 && x <= 85) || (x >= 190 && x <= 260)) && (y >= 160 && y <= 360)){
                [self loadDataBodyList: @"上肢"];
            }else if ((x >= 95 && x <= 195) && (y >= 337 && y <= 567)){
                [self loadDataBodyList: @"下肢"];
            }
        }else{
            if (((x >= 110 && x <= 190) && (y >= 160 && y <= 260))){
                [self loadDataBodyList: @"背部"];
            }else if ((x >= 117 && x <= 170) && (y >= 280 && y <= 328)){
                [self loadDataBodyList: @"排泄部"];
            }
        }
    }else{ // 5s/5屏幕
        if (self.turnBtnType == TurnButtonTypeFace) {
            if ((x >= 80 && x <= 120) && (y >= 43 && y <= 70)) {
                [self loadDataBodyList: @"头部"];
            }else if ((x >= 80 && x <= 120) && (y >= 71 && y <= 90)){
                [self loadDataBodyList: @"耳眼口鼻"];
            }else if ((x >= 85 && x <= 115) && (y >= 95 && y <= 110)){
                [self loadDataBodyList: @"颈部"];
            }else if ((x >= 70 && x <= 130) && (y >= 120 && y <= 160)){
                [self loadDataBodyList: @"胸部"];
            }else if ((x >= 75 && x <= 125) && (y >= 162 && y <= 200)){
                [self loadDataBodyList: @"腹部"];
            }else if ((x >= 80 && x <= 120) && (y >= 205 && y <= 245)){
                [self loadDataBodyList: @"生殖器"];
            }else if (((x >= 25 && x <= 60) || (x >= 138 && x <= 225)) && (y >= 115 && y <= 255)){
                [self loadDataBodyList: @"上肢"];
            }else if ((x >= 65 && x <= 130) && (y >= 248 && y <= 400)){
                [self loadDataBodyList: @"下肢"];
            }
        }else{
            if (((x >= 75 && x <= 135) && (y >= 120 && y <= 200))){
                [self loadDataBodyList: @"背部"];
            }else if ((x >= 80 && x <= 120) && (y >= 205 && y <= 245)){
                [self loadDataBodyList: @"排泄部"];
            }
        }
    }
}

// 加载部位列表
- (void)loadBodyList
{
    NSDictionary *cookie = @{@"cookie" : [UserDefaultsTool stringForKey:XHToken]};
    NSDictionary *params = nil;
    
    [HTTPSTool POSTRequestWithPath: XHGetGuideBody cookie: cookie params: params success:^(NSDictionary * _Nonnull JSON) {
        
        self.bodyLists =[IntelligentBody mj_objectArrayWithKeyValuesArray:JSON[@"data"][@"list"]];
//        XHLog(@"JSON: %@", JSON);
        [self.tableView reloadData];
    }];
}


// 转身按钮事件
- (void)turnBtnClick
{
    if (self.buttonType == ButtonTypeMan) {
        if (self.turnBtnType == TurnButtonTypeFace) {
            self.imageView.image=[UIImage imageNamed:@"男反面.png"];
            self.turnBtnType = TurnButtonTypeOpposite;
        }else{
            self.imageView.image=[UIImage imageNamed:@"男正面.png"];
            self.turnBtnType = TurnButtonTypeFace;
        }
    }else if (self.buttonType == ButtonTypeWoman){
        if (self.turnBtnType == TurnButtonTypeFace) {
            self.imageView.image=[UIImage imageNamed:@"女反面.png"];
            self.turnBtnType = TurnButtonTypeOpposite;
        }else{
            self.imageView.image=[UIImage imageNamed:@"女正面.png"];
            self.turnBtnType = TurnButtonTypeFace;
        }
    }else if (self.buttonType == ButtonTypeChild){
        if (self.turnBtnType == TurnButtonTypeFace) {
            self.imageView.image=[UIImage imageNamed:@"儿童反面.png"];
            self.turnBtnType = TurnButtonTypeOpposite;
        }else{
            self.imageView.image=[UIImage imageNamed:@"儿童正面.png"];
            self.turnBtnType = TurnButtonTypeFace;
        }
    }
    // 切换图片动画
    [self imageAnimation];
 
}

// 点击头像跳转到自测界面
- (void)loadDataBodyList: (NSString *)bodyName
{
    GuideSymptomViewController *guideSymptomViewController = [[GuideSymptomViewController alloc] init];

    for (IntelligentBody *intelligentBody in self.bodyLists) {
        if ([intelligentBody.bodyName isEqualToString:bodyName]) {
            
            guideSymptomViewController.intelligentBody = intelligentBody;
        }
    }
    guideSymptomViewController.sex= [NSString stringWithFormat:@"%lu", self.buttonType + 1];
    
    guideSymptomViewController.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:guideSymptomViewController animated:YES];
}

// 切换图片动画
- (void)imageAnimation{
    // 创建动画
    CATransition *anim = [CATransition animation];
    // 动画类型,效果 paegCurl翻页 cube3D旋转  cube  kCATransitionPush kCATransitionMoveIn
    anim.type = @"flip";
    // 动画类型方法
    anim.subtype = kCATransitionFromLeft;
    
    anim.duration = 0.8;
    
    [self.imageView.layer addAnimation:anim forKey:nil];
    
}
#pragma mark - UITableViewDelegate/UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bodyLists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    IntelligentBody *intelligentBody = self.bodyLists[indexPath.row];
    
    cell.textLabel.text = intelligentBody.bodyName;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    IntelligentBody *intelligentBody = self.bodyLists[indexPath.row];
    GuideSymptomViewController *guideSymptomViewController = [[GuideSymptomViewController alloc] init];
    guideSymptomViewController.intelligentBody = intelligentBody;
    guideSymptomViewController.sex=[NSString stringWithFormat:@"%lu", self.buttonType + 1];
    guideSymptomViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:guideSymptomViewController animated:YES];
}

#pragma mark - scrollViewDelegate
//修改分页的小点
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollView) {
        // 计算页数
        int page = scrollView.contentOffset.x / self.scrollView.bounds.size.width;
        self.pageControl.currentPage = page;
        self.segMentControl.selectedSegmentIndex = page;
    }
}

- (IBAction)segmentSelect:(UISegmentedControl *)sender {

    if (self.segMentControl.selectedSegmentIndex==0) {
        self.pageControl.currentPage = self.segMentControl.selectedSegmentIndex;
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }
    else
    {
        self.pageControl.currentPage = self.segMentControl.selectedSegmentIndex;
        self.scrollView.contentOffset = CGPointMake(ScreenW, 0);
    }
}


@end
